package com.amit.hibernate;

import java.util.ArrayList;
import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateCandidateDAO implements CandidateDAO {

	@Override
	public void addCandidate(Candidate can) {
		
		CHibernateTemplate.save(can);
		
		
	}

	@Override
	public Candidate getCandiadteByCid(int i) {
		
		
		Candidate can=(Candidate)CHibernateTemplate.load(Candidate.class,i);
		
		return can;
	}

	@Override
	public void deleteCandidate(int i) {
		CHibernateTemplate.delete(Candidate.class,i);
		
		
	}

	@Override
	public void updateCandidate(Candidate can) {
		
		CHibernateTemplate.update(can);
	}

	@Override
	public List<Candidate> getAllCandidates() {
		
		List<Candidate> cans=new ArrayList<Candidate>();
		try
		{
		
			SessionFactory sf=ChibernateUtil.getfactory();
			Session session=sf.openSession();
			Transaction tx= session.beginTransaction();
			String Hql="from Candidate can";
			Query q=session.createQuery(Hql);
			cans=q.list();
			tx.commit();
			session.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return cans;
	}
	

	

}
